<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" :width="1400">
    <el-form
      class="mb-10px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="74px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件名称" prop="partName">
        <el-input
          v-model="queryParams.partName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTableRef"
      v-loading="loading"
      :data="pieceData"
      border
      style="width: 100%"
      :max-height="450"
      @select="handleSelectionChange"
      class="table-style"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="订单类型" align="center" prop="dictOrderType">
        <template #default="scope">
          <dict-tag type="market_order_type" :value="scope.row.dictOrderType" />
        </template>
      </el-table-column>
      <el-table-column label="部件编码" align="center" prop="partNumber" />
      <el-table-column label="部件名称" align="center" prop="partName" />
      <el-table-column label="部件规格" align="center" prop="partSpecification" />
      <el-table-column label="订单数量" align="center" prop="orderRemaQuantity" />
      <el-table-column label="单位" align="center" prop="partUnit"/>
      <el-table-column label="客户代码" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="产品编码" align="center" prop="productNumber" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品规格" align="center" prop="productStandard" />
      <el-table-column label="图纸图号" align="center" prop="drawingNumber" />
      
    </el-table>
    <!-- 分页 -->
    <div class="mb-20px">
      <el-pagination
        v-model:current-page="queryParams.pageNo"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50, 100]"
        :background="true"
        class="float-right mt-5px mb-20px"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ProductionIncomingApi from '@/api/produce/productManagement/productionIncoming'

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const pieceData = ref([]) // 数据
const total = ref(0) // 列表的总页数v
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNumber: undefined,
  partName: undefined
})
const queryFormRef = ref() // 搜索的表单
const order = ref()

/** 打开弹窗 */
const open = async (val) => {
  dialogVisible.value = true
  order.value = val
  // 修改时，设置数据
  loading.value = true
  try {
    const data = await ProductionIncomingApi.getSelectPart(queryParams)
    pieceData.value = data.list
    total.value = data.total
    nextTick(() => {
      pieceData.value.map(item => {
        if(val === item['orderNumber']) {
          multipleTableRef.value!.toggleRowSelection(item, true)
        }
    })
  })
  } finally {
    loading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  loading.value = true
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', checkedGh.value)
  } finally {
    loading.value = false
  }
}

/** 页面容量 */
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  open(order.value)
}
/** 页面 */
const handleCurrentChange = (val: number) => {
  queryParams.pageNo = val
  open(order.value)
}

/**单选 */
const checkedGh = ref()
const multipleTableRef = ref()
const handleSelectionChange = (selection, row) => {
  multipleTableRef.value.clearSelection()
  if (selection.length === 0) {
    checkedGh.value = []
    return
  }
  checkedGh.value = row
  multipleTableRef.value.toggleRowSelection(row, true)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  open(order.value)
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
</script>
<style lang="scss" scoped>
.table-style ::v-deep .el-table-column--selection.is-leaf .el-checkbox {
  display: none;
}
</style>
